package com.wind.cqboot.plugin;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.wind.cqboot.utils.MsgUtil;
import com.wind.cqboot.utils.HttpUtil;

import cn.hutool.core.util.StrUtil;
import net.lz1998.cq.event.message.CQGroupMessageEvent;
import net.lz1998.cq.robot.CQPlugin;
import net.lz1998.cq.robot.CoolQ;
import net.lz1998.cq.utils.CQCode;

@Component
public class MlRbootPlugin extends CQPlugin
{
    @Override
    public int onGroupMessage(CoolQ cq, CQGroupMessageEvent event)
    {
        String msg = event.getMessage();
        Long groupId = event.getGroupId();
        Long userId = event.getUserId();
        if (msg.contains(CQCode.at(cq.getSelfId())))
        {
            String userMsg = MsgUtil.getAtMsg(cq, msg);
            if (StrUtil.isBlank(userMsg))
            {
                return MsgUtil.sendReply(cq, groupId, userId, "你得说点什么我才懂啊");
            }
            String content = botMessageReply(userMsg);
            if (StrUtil.isNotBlank(content))
            {
                MsgUtil.sendReply(cq, groupId, userId, content);
            }
            return MESSAGE_BLOCK;
        }
        // 继续执行下一个插件
        return MESSAGE_IGNORE;
    }

    @Value("${rebot.ml.apikey}")
    private String apikey;

    @Value("${rebot.ml.secret}")
    private String secret;

    public String botMessageReply(String msg)
    {
        String params = "question=" + msg + "&api_key=" + apikey + "&api_secret=" + secret;
        // 只实现了基础的 具体的请自行修改
        return bulid(msg, params);
    }

    private static String bulid(String content, String params)
    {
        String result = HttpUtil.sendGet("http://i.itpk.cn/api.php", params);
        JsonObject json;
        StringBuffer sb = new StringBuffer();
        String text = "";
        switch (content)
        {
            case "笑话":
                json = new JsonParser().parse(result).getAsJsonObject();
                sb.append("标题：" + json.get("title") + "\n");
                sb.append("内容：" + json.get("content").toString().replaceAll("\\\\r|\\\\n|&nbsp;", "\n"));
                text = sb.toString();
                break;
            case "观音灵签":
                json = new JsonParser().parse(result).getAsJsonObject();
                sb.append("您抽取的是第" + json.get("number2") + "签\n");
                sb.append("签位：" + json.get("haohua") + "\n");
                sb.append("签语：" + json.get("qianyu") + "\n");
                sb.append("诗意：" + json.get("shiyi") + "\n");
                sb.append("解签：" + json.get("jieqian"));
                text = sb.toString();
                break;
            case "月老灵签":
                json = new JsonParser().parse(result).getAsJsonObject();
                sb.append("您抽取的是第" + json.get("number2") + "签\n");
                sb.append("签位：" + json.get("haohua") + "\n");
                sb.append("签语：" + json.get("shiyi") + "\n");
                sb.append("解签：" + json.get("jieqian"));
                text = sb.toString();
                break;
            case "财神爷灵签":
                json = new JsonParser().parse(result).getAsJsonObject();
                sb.append("您抽取的是第" + json.get("number2") + "签\n");
                sb.append("签语：" + json.get("qianyu") + "\n");
                sb.append("注释：" + json.get("zhushi") + "\n");
                sb.append("解签：" + json.get("jieqian") + "\n");
                sb.append("解说：" + json.get("jieshuo") + "\n");
                sb.append("婚姻：" + json.get("hunyin") + "\n");
                sb.append("事业：" + json.get("shiye") + "\n");
                sb.append("求财：" + json.get("qiucai"));
                text = sb.toString();
                break;
            case "指令":
                text = mainMenu();
                break;
            case "ip":
                text = mainMenu();
                break;
            default:
                text = result;
                break;
        }
        return text;
    }

    public static String mainMenu()
    {
        StringBuffer sb = new StringBuffer();
        sb.append("指令:\n");
        sb.append("地区天气 \n");
        sb.append("天气\n");
        sb.append("@tq您的ip地址\n");
        sb.append("@ip您的ip地址\n");
        // sb.append("ip\n");
        sb.append("@qq要查询的QQ号码\n");
        sb.append("@lol英雄联盟大区--角色名称\n");
        sb.append("@sfz身份证号码\n");
        sb.append("@sjh手机号码\n");
        sb.append("@cy四字成语\n");
        sb.append("笑话\n");
        sb.append("观音灵签\n");
        sb.append("月老灵签\n");
        sb.append("财神爷灵签");
        return sb.toString();
    }
}
